﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Model;

namespace BusinessLogic
{
    public class Workspace{
        private List<string> _guideWordList = new List<string>();
        private Dictionary<string, List<string>> _resonList = new Dictionary<string, List<string>>();
//        private string[,] _rmMatrix = new string[,]{
//                                                        {"E","E","E","E","D"},
//                                                        {"E","E","E","D","C"},
//                                                        {"E","E","D","C","B"},
//                                                        {"E","D","C","B","A"},
//                                                        {"D","C","B","A","A"}
//                                                     };

        public Project project = Project.ProjectInstance;

        private static List<string> _hideColumns = new List<string>();
        private readonly static List<string> _allColumns = new List<string>() { "引导词", "偏离", "原因", "可能的后果", "现有安全保障", "S1", "L1"
        ,"RR1","建议类别","建议编号","建议措施","S2","L2","RR2","负责人","计划完成日期"};

        public static List<string> AllColumns{
            get { return _allColumns; }
        }

        public List<string> GetHideColumns(){
            return _hideColumns;
        }

        public List<string> GetAllColumns(){
            return _allColumns;
        }

        public event Action<List<string>> ColumnsChanged;
        public event Action<Dictionary<string ,string >> PidListChanged;

        public void OnColumnsChanged(List<string> s){
            if (ColumnsChanged != null){
                ColumnsChanged(s);
            }
        }

        public void OnPidListChanged(Dictionary<string ,string > d)
        {
            if (PidListChanged != null)
            {
                PidListChanged(d);
            }
        }

        public void HideColumns(List<string> cols){
            _hideColumns.Clear();
            foreach (string s in _allColumns){
                if (!cols.Contains(s)){
                    _hideColumns.Add(s);
                }
            }
            OnColumnsChanged(_hideColumns);
        }

        public Workspace(){}

        public List<string> GuideWordList{
            get { return _guideWordList; }
            set { _guideWordList = value; }
        }

        public Dictionary<string, List<string>> ResonList{
            get { return _resonList; }
            set { _resonList = value; }
        }

//        public string[,] RmMatrix{
//            get { return _rmMatrix; }
//            set { _rmMatrix = value; }
//        }

        public void InitProjectInstance(string comName,string proName, string proLoc, string TecUni,string hazType,string proDes){
            ProjectDetails projectDetails = new ProjectDetails(comName, proName, proLoc, TecUni, hazType, proDes);
            RiskMatrix riskMatrix = new RiskMatrix();
            List<HazopTeam> hazopTeams = new List<HazopTeam>();
            project.ProjectDetails = projectDetails;
            project.RiskMatrix = riskMatrix;
            project.HazopTeam = hazopTeams;
        }


        public List<string> GetPidTitleList(){
            List<string> titleList = new List<string>();
            foreach (PID pid in project.PidList){
                titleList.Add(pid.Title);
            }
            return titleList;
        }

        public List<string> GetPidNumList() {
            List<string> res = new List<string>();
            foreach (PID pid in project.PidList) {
                res.Add(pid.DrawingNum);
            }
            return res;
        }

        public List<string> GetPsiList(){
            return project.PsiList;
        }

        public void SavePsiList(List<string> list){
            project.PsiList.Clear();
            project.PsiList = list;
        }

        public string [,] GetRiskMatrix(){
            return project.RiskMatrix.RmMatrix;
        }
        

        public WorkNode GetWorkNodeByNodeName(string name){
            foreach (WorkNode node in project.WorkNodeList){
                if (node.NodeName.Equals(name)){
                    return node;
                }
            }
            return null;
        }

        public void AddNewWorkNode(WorkNode workNode){
            project.WorkNodeList.Add(workNode);
        }

        public bool HasNode(string name){
            foreach (WorkNode workNode in project.WorkNodeList){
                if (workNode.NodeName.Equals(name)){
                    return true;
                }
            }
            return false;
        }

        public List<WorkNodeItem> GetAllWorkNodeItemsByNodeName(string name){
            foreach (WorkNode node in project.WorkNodeList){
                if (node.NodeName.Equals(name)){
                    return node.WorkNodeItemList;
                }
            }
            return null;
        }

        public string CalculateMatrix(string s,string l) {
            int ss = Int32.Parse(s) - 1;
            int ll = Int32.Parse(l) - 1;
            if (ss < 0 || ss > 4 || ll < 0 || ll > 4)
                return string.Empty;
            return project.RiskMatrix.RmMatrix[ll,ss];
        }

        public void SaveWorkNode(WorkNode workNode){
           
        }

        private List<WorkNodeItem> DatatableToWorkNodeItems(DataTable dataTable){
            List<WorkNodeItem> list = new List<WorkNodeItem>();
            return list;
        }

        public void SaveProject(string fileName){
            BinaryFormatter binFormatter = new BinaryFormatter();
            Stream fstream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
            binFormatter.Serialize(fstream,project);
            fstream.Close();
        }

        public Project OpenProject(string fileName){
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            Stream fstream = File.OpenRead(fileName);
            project = (Project) binaryFormatter.Deserialize(fstream);
            fstream.Close();
            return project;
        }

        public bool IsProjectFileExists(string fileName){
            return File.Exists(fileName);
        }

        public void ExportToExcel(string fileName,Project project,List<string> cols) {
            DataAccess.CSVHelper.ExportProjectToExcel(fileName,project,cols);
        }

        public int GetAdviceCountByNodeName(string name) {
            int count = 0;
            WorkNode node = GetWorkNodeByNodeName(name);
            if (node == null) {
                return 0;
            }
            foreach (WorkNodeItem item in node.WorkNodeItemList) {
                if (item.Recommendations != null) {
                    count++;
                }
            }
            return count;
        }

        private static Dictionary<string, List<WhatIfAskItem>> _hazardList;
        private static List<string> _titleList = new List<string>();

        public  Dictionary<string, List<WhatIfAskItem>> HazardList{
            get { return _hazardList; }
            set { _hazardList = value; }
        }

        public List<string> TitleList{
            get { return _titleList; }
            set { _titleList = value; }
        }


        static Workspace(){
            _hazardList = DataAccess.CSVHelper.GetAskItems2("./Config/典型工艺危害.xls");
            _titleList = DataAccess.CSVHelper.GetTitleList();
        }
    }
}

